﻿using System;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Data;

using Light.Utility;
using Light.Framework.Castle;
using Light.Framework.Contract;
using Light.Framework.Model;


namespace Light.Framework.Web
{
    /// <summary>
    /// Web页面基类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="ITComponent"></typeparam>
    public class BasePage<T, ITComponent> : Page
        where T : class, IEntity, new()
        where ITComponent : IComponent<T>
    {

        protected OperateResult Result = new OperateResult();

        /// <summary>
        /// 当前实体组件
        /// </summary>
        protected virtual ITComponent IComponent
        {
            get
            {
                return ServerApplication.ComponentFactory.Create<ITComponent>();
            }
        }


        protected void TransmitFile(string filePath)
        {
            FileInfo fileInfo = new FileInfo(filePath);
            if (!fileInfo.Exists)
                return;

            string contentType = null;
            switch (fileInfo.Extension.ToLower())
            {
                case ".txt":
                case ".ini":
                case ".log":
                    contentType = "text/plain";
                    break;
                case ".xml":
                    contentType = "text/xml";
                    break;
                case ".zip":
                    contentType = "application/x-zip-compressed";
                    break;
                case ".pdf":
                    contentType = "application/pdf";
                    break;
                case ".doc":
                    contentType = "application/msword";
                    break;
                case ".xls":
                    contentType = "application/vnd.ms-excel";
                    break;
                case ".ppt":
                case ".pps":
                    contentType = "application/vnd.ms-powerpoint";
                    break;
                case ".tif":
                    contentType = string.Format("image/tiff;name=\"{0}\"", HttpUtility.HtmlAttributeEncode(fileInfo.Name));
                    break;
                case ".jpg":
                    contentType = string.Format("image/jpeg;name=\"{0}\"", HttpUtility.HtmlAttributeEncode(fileInfo.Name));
                    break;
                case ".png":
                case ".gif":
                case ".bmp":
                    contentType = string.Format("image/{0};name=\"{1}\"", fileInfo.Extension.TrimStart('.'), HttpUtility.HtmlAttributeEncode(fileInfo.Name));
                    break;
                //case ".htm":
                //case ".html":
                //case ".asp":
                //case ".aspx":
                default:
                    break;
            }

            try
            {
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();

                //Response.Charset = "gb2312";
                Response.ContentType = contentType;

                Response.AddHeader("content-disposition", string.Format("attachment;filename=\"{0}\"", HttpUtility.UrlEncode(fileInfo.Name)));

                //Response.Write("<html><head><meta http-equiv=Content-type content=\"text/html;charset=utf-8>");
                //Response.AddHeader("content-disposition", string.Format("inline;filename=\"{0}\"", HttpUtility.UrlEncode(fileInfo.Name)));

                Response.TransmitFile(filePath);

                //Response.Write("</body></html>");
                Response.Flush();
            }
            finally
            {
                File.Delete(filePath);
                Response.End();
            }
        }

        protected void TransmitFile(DataSet dataset, DataFileType fileType)
        {
            if (dataset != null)
            {
                if (fileType == DataFileType.TEXT && dataset.Tables.Count != 0)
                {
                    this.TransmitFile(dataset.Tables[0], fileType);
                }
                else
                {
                    string tempFileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
                    string tranFileName = dataset.DataSetName;

                    string contentType = null;
                    switch (fileType)
                    {
                        case DataFileType.XML:
                            tempFileName += ".xml";
                            tranFileName += ".xml";
                            contentType = "text/xml";
                            break;
                        case DataFileType.EXCEL:
                            tempFileName += ".xls";
                            tranFileName += ".xls";
                            contentType = "application/vnd.ms-excel";
                            break;
                        default:
                            return;
                    }

                    FileHelper helper = new FileHelper();
                    helper.Export(dataset, tempFileName, fileType);

                    this.TransmitFile(contentType, tranFileName, tempFileName);
                }
            }
        }

        protected void TransmitFile(DataTable table, DataFileType fileType)
        {
            if (table != null)
            {
                string tempFileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
                string tranFileName = table.TableName;

                string contentType = null;
                switch (fileType)
                {
                    case DataFileType.TEXT:
                        tempFileName += ".txt";
                        tranFileName += ".txt";
                        contentType = "text/plain";
                        break;
                    case DataFileType.XML:
                        tempFileName += ".xml";
                        tranFileName += ".xml";
                        contentType = "text/xml";
                        break;
                    case DataFileType.EXCEL:
                        tempFileName += ".xls";
                        tranFileName += ".xls";
                        contentType = "application/vnd.ms-excel";
                        break;
                    default:
                        return;
                }

                FileHelper helper = new FileHelper();
                helper.Export(table, tempFileName, fileType);

                this.TransmitFile(contentType, tranFileName, tempFileName);
            }
        }

        private void TransmitFile(string contentType, string tranFileName, string tempFileName)
        {
            try
            {
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();

                Response.ContentType = contentType;
                Response.AddHeader("content-disposition", string.Format("attachment;filename={0}", HttpUtility.UrlEncode(tranFileName)));
                Response.TransmitFile(tempFileName);
                Response.Flush();
            }
            finally
            {
                File.Delete(tempFileName);
                Response.End();
            }
        }
    }
}
